package com.lingyu.jsjx.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyu.jsjx.utils.ExcelImportUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.lingyu.jsjx.domain.CoursePlan;
import com.lingyu.jsjx.service.ICoursePlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教学规划
 * Controller
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/coursePlan")
public class CoursePlanController extends BaseController {
    @Autowired
    private ICoursePlanService coursePlanService;

    /**
     * 查询教学规划
     * 列表
     */
    @RequiresPermissions("jsjx:coursePlan:list")
    @GetMapping("/list")
    public TableDataInfo list(CoursePlan coursePlan) {
        startPage();
        List<CoursePlan> list = coursePlanService.selectCoursePlanList(coursePlan);
        return getDataTable(list);
    }

    /**
     * 导出教学规划
     * 列表
     */
    @RequiresPermissions("jsjx:coursePlan:export")
    @Log(title = "教学规划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CoursePlan coursePlan) {
        List<CoursePlan> list = coursePlanService.selectCoursePlanList(coursePlan);
        ExcelUtil<CoursePlan> util = new ExcelUtil<CoursePlan>(CoursePlan.class);
        util.exportExcel(response, list, "教学规划数据");
    }

    /**
     * 获取教学规划
     * 详细信息
     */
    @RequiresPermissions("jsjx:coursePlan:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId) {
        return success(coursePlanService.selectCoursePlanByPlanId(planId));
    }

    /**
     * 新增教学规划
     */
    @RequiresPermissions("jsjx:coursePlan:add")
    @Log(title = "教学规划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CoursePlan coursePlan) {
        QueryWrapper<CoursePlan> queryWrapper = new QueryWrapper<>();
        CoursePlan courseName = coursePlanService.getOne(queryWrapper.eq("course_name", coursePlan.getCourseName()));
        if (courseName != null) {
            return error("课程名称已存在");
        }
        return toAjax(coursePlanService.insertCoursePlan(coursePlan));
    }

    /**
     * 修改教学规划
     */
    @RequiresPermissions("jsjx:coursePlan:edit")
    @Log(title = "教学规划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CoursePlan coursePlan) {
        QueryWrapper<CoursePlan> queryWrapper = new QueryWrapper<>();
        CoursePlan courseName = coursePlanService.getOne(queryWrapper.eq("course_name", coursePlan.getCourseName()));
        if (courseName != null) {
            return error("课程名称已存在");
        }
        return toAjax(coursePlanService.updateCoursePlan(coursePlan));
    }

    /**
     * 删除教学规划
     */
    @RequiresPermissions("jsjx:coursePlan:remove")
    @Log(title = "教学规划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds) {
        return toAjax(coursePlanService.deleteCoursePlanByPlanIds(planIds));
    }

    @GetMapping(value = "/getCourseStatusStatistic")
    public AjaxResult getCourseStatusStatistic() {
        JSONObject result = new JSONObject();
        CoursePlan coursePlan = new CoursePlan();
        coursePlan.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        coursePlan.setCourseStatus(47L);//待开课
        int waitStart = coursePlanService.selectCoursePlanList(coursePlan).size();
        coursePlan.setCourseStatus(48L);//进行中
        int continuing = coursePlanService.selectCoursePlanList(coursePlan).size();
        coursePlan.setCourseStatus(49L);//已完成
        int completed = coursePlanService.selectCoursePlanList(coursePlan).size();
        result.put("waitStart", waitStart);
        result.put("continuing", continuing);
        result.put("completed", completed);
        return success(result);
    }

    /**
     * 统计课程状态数量
     */
    @GetMapping(value = "/queryCourseStatusCount")
    public AjaxResult queryCourseStatusCount() {
        return success(coursePlanService.selectCourseStatusCount());
    }

    @Log(title = "教学规划", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<CoursePlan> util = new ExcelUtil<>(CoursePlan.class);
        List<CoursePlan> coursePlanList = util.importExcel(file.getInputStream());
        String message = coursePlanService.importCoursePlan(coursePlanList, false);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CoursePlan> util = new ExcelUtil<>(CoursePlan.class);
        util.importTemplateExcel(response, "课程计划数据");
    }

//    @Log(title = "教学规划", businessType = BusinessType.IMPORT)
//    @PostMapping("/importData")
//    public AjaxResult importData(MultipartFile file, @RequestParam("scoreId") Long scoreId) throws Exception {
//        ExcelImportUtil.importExcelData(file, dataList -> {
//            try {
//                this.processStudentDataList(dataList);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });
//        return success();
//    }
//
//    private void processStudentDataList(List<Map<String, String>> dataList) throws Exception {
//        for (Map<String, String> rowData : dataList) {
//            if (!rowData.containsKey("课程名称") || rowData.get("课程名称").isEmpty()) {
//                throw new Exception("课程名称不能为空");
//            }
//
//            if (!rowData.containsKey("课程类型") || rowData.get("课程类型").isEmpty()) {
//                throw new Exception("课程类型字段不能为空");
//            }
//
//            String name = rowData.get("课程名称");
//            String studentNo = rowData.get("课程类型");
//            CoursePlan coursePlan = new CoursePlan();
//
//            int result = coursePlanService.insertCoursePlan(coursePlan);
//            if (result < 0) {
//                throw new Exception(name + "插入失败");
//            }
//        }
//
//
//    }
}
